package com.zlsy.secondtime.background;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouliang
 * @date 2020/5/25 20:30
 */
public class MySubject {

    /**
     * 定义观察者集合
     */
    private List<MyObserver> observers = new ArrayList<MyObserver>();
    /**
     * 状态
     */
    private int state;

    public int getState() {
        return state;
    }

    //设置状态
    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(MyObserver observer) {
        observers.add(observer);
    }

    public void notifyAllObservers() {
        //遍历每个观察者执行动作
        for (MyObserver observer : observers) {
            observer.update();
        }
    }
}
